<template>
  <div class="login-container" >
    <!-- 自定义顶部栏 -->
    <div class="custom-title-bar">
  <div class="title-left">
    <img class="title-logo" src="@/assets/icons/tray-1.png" alt="logo" />
    <span class="title-text">SIX</span>
  </div>
  <div class="window-controls">
    <el-icon @click="minimizeWindow"><Minus /></el-icon>
    <el-icon @click="toggleMaximizeWindow"><FullScreen /></el-icon>
    <el-icon @click="closeWindow"><Close /></el-icon>
  </div>
</div>

    <div class="login-card">
      <img
        class="avatar"
        :class="{ spinning: isSpinning }"
        src="@/assets/avatar.png"
        alt="头像"
        @click="spinAvatar"
      />
      
      <div class="title-wrapper">
        <img class="logo" src="@/assets/icons/tray-1.png" alt="SIX" />
        <h2 class="title">欢迎登录 SIX 聊天</h2>
      </div>


      <el-form :model="user.userInfo" :rules="rules" ref="formRef" label-position="top">
        <el-form-item prop="username">
          <el-input
            v-model="user.userInfo.username"
            prefix-icon="el-icon-user"
            placeholder="请输入账号"
            class="input-item"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="user.userInfo.password"
            prefix-icon="el-icon-lock"
            show-password
            placeholder="请输入密码"
            class="input-item"
          />
        </el-form-item>
      </el-form>

      <el-checkbox v-model="user.userInfo.remember">记住密码</el-checkbox>
      <el-checkbox v-model="user.userInfo.autoLogin" style="margin-left: 10px">自动登录</el-checkbox>

      <div class="button-group">
        <el-button type="primary" @click="handleLogin" round>登录</el-button>
        <el-button type="success" @click="openRegisterWindow" round>注册</el-button>
      </div>
    </div>
  </div>
</template>
<script setup>
import { onMounted , ref } from 'vue';
import { useUserStore } from '@/stores/user';
import request from '@/axios/index';
import { Minus, Close,FullScreen  } from '@element-plus/icons-vue';

const user = useUserStore();
const isSpinning = ref(false);
const formRef = ref(null);
let isMaximized = false;

//最大化窗口
const toggleMaximizeWindow = () => {
  window.electronAPI?.ipcRenderer?.send('toggle-maximize');
  isMaximized = !isMaximized;
};

// 登录校验规则
const rules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, message: '账号长度不能少于3位', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' },
  ],
};

const spinAvatar = () => {
  if (isSpinning.value) return;
  isSpinning.value = true;
  setTimeout(() => {
    isSpinning.value = false;
  }, 2000);
};

const handleLogin = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      console.log("进入到登录");
      request.post('user/login', {
        username: user.userInfo.username,
        password: user.userInfo.password,
      }).then((res) => {
        console.log("登录成功:", res);
        window.electronAPI.saveToken(res);
        user.setUserInfo({
          username: user.userInfo.username, // 使用表单中的值
          remember: user.userInfo.remember,
          autoLogin: user.userInfo.autoLogin
        });
        console.log("username:",user.userInfo.username)
        // 登录成功后建立 socket 连接

        window.electronAPI.socket.connect('172.20.10.3', 5209,user.userInfo.username).then((result) => {
          console.log("Socket 连接成功:", result);
            // // 通知主进程跳转聊天窗口
            window.electronAPI.ipcRenderer.send('login-success', {
            username: user.userInfo.username,
            password: user.userInfo.password,
          });

        }).catch((err) => {
          console.error("Socket 连接失败:", err);
        });

      }).catch((err) => {
        console.error("登录失败:", err);
      });
    }
  });
};


const openRegisterWindow = () => {
  window.electronAPI?.ipcRenderer?.send('open-register-window');
};

window.electronAPI?.ipcRenderer?.on('login-fail', (_, message) => {
  // 这里还是保留一下提示（也可以做个红字提示）
  // ElMessage.error(message || '登录失败');
});

// 自定义标题栏按钮操作
const minimizeWindow = () => {
  window.electronAPI?.ipcRenderer?.send('minimize-window');
};
const closeWindow = () => {
  window.electronAPI?.ipcRenderer?.send('close-window');
};

</script>
<style scoped>
.custom-title-bar {
  height: 32px;
  -webkit-app-region: drag;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;
  background: rgba(255, 255, 255, 0.05);
  color: #fff;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
}

.title-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.title-logo {
  width: 20px;
  height: 20px;
  -webkit-app-region: no-drag;
}

.title-text {
  font-weight: bold;
  font-size: 14px;
  -webkit-app-region: no-drag;
}

.window-controls {
  display: flex;
  gap: 12px;
  -webkit-app-region: no-drag;
}

.window-controls .el-icon {
  cursor: pointer;
  font-size: 18px;
  transition: color 0.2s;
}

.window-controls .el-icon:hover {
  color: #ff4d4f;
}

.login-container {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  /* padding-top: 32px; */
  background: linear-gradient(to right, #00c6ff, #0072ff);
  font-family: "Segoe UI", sans-serif;
}

.login-card {
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  padding: 40px;
  width: 350px;
  text-align: center;
  /* box-shadow: 0 8px 32px rgba(31, 38, 135, 0.37); */
}
.title-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
}

.logo {
  width: 32px;
  margin-right: 10px; /* 用这个代替 margin-bottom */
  vertical-align: middle; 
  transform: translateY(2px); /* 微调垂直位置 */
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  border: 2px solid #fff;
  margin: 10px 0 20px;
  transition: transform 2s linear;
}

.avatar.spinning {
  transform: rotate(720deg);
}

.title {
  color: #fff;
  font-size: 1.8rem;
  margin-bottom: 20px;
}

.input-item {
  margin-bottom: 16px;
}

.button-group {
  margin-top: 20px;
  display: flex;
  justify-content: space-around;
}
</style>